/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.core.service;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.core.dao.DeptDao;
import com.tech15.core.dao.SysUserDao;
import com.tech15.core.dao.SysYgDao;
import com.tech15.core.model.Dept;
import com.tech15.core.model.SysUser;
import com.tech15.core.model.SysYg;
import com.tech15.core.vo.query.SysYgQuery;
import com.tech15.pub.dao.PubDataDao;
import com.tech15.pub.model.PubData;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class SysYgManager extends BaseManager<SysYg,String>{

	private SysYgDao sysYgDao;
	private DeptDao deptDao;
	private PubDataDao pubDataDao;
	private SysUserDao sysUserDao;
	
	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}

	public void setPubDataDao(PubDataDao pubDataDao) {
		this.pubDataDao = pubDataDao;
	}
	
	public void setDeptDao(DeptDao deptDao) {
		this.deptDao = deptDao;
	}
	public void setSysYgDao(SysYgDao dao) {
		this.sysYgDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.sysYgDao;
	}
	
	@Transactional(readOnly=true)
	public SysYg getById(String id){
		SysYg yg = sysYgDao.getById(id);
		if(yg.getDeptid() != null){
			Dept dept = deptDao.getById(yg.getDeptid());
			if(dept != null){
				yg.setDeptname(dept.getDeptname());
			}
		}
		if(yg.getSzfc() != null){
			Dept dept = deptDao.getById(yg.getSzfc());
			if(dept != null){
				yg.setFcmc(dept.getDeptname());
			}
		}
		if(ObjectUtils.isNotEmpty(yg.getZt())){
			PubData pd = pubDataDao.getById(yg.getZt());
			yg.setDataname(pd.getName());
		}
		return yg; 
	}
	
	@Transactional(readOnly=true)
	public Page findPage(SysYgQuery query) {
		return sysYgDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return sysYgDao.findByPageRequest(pr);
	}
	
	public void updateYg(SysYg yg){
		sysYgDao.update(yg);
		SysUser user = sysUserDao.getByUsername(yg.getYgbh());
		if(user != null){
			user.setDeptid(yg.getDeptid());
			user.setDeptname(yg.getDeptname());
			sysUserDao.update(user);
		}
	}
	
}
